package com.zhny.aiot.rainfall.plugin;

import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.modbus.ModbusNumDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemRequestMessage;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.TaskEntity;
import com.zhny.aiot.rainfall.plugin.model.RainfallStoreEntry;

public class RainfallDevice extends ModbusNumDevice<RainfallStoreEntry> {

    public RainfallDevice(ModemEngine engine, IDeviceFactory<ModemConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super (engine, factory, centerFacilities,entity);
    }

    @Override
    public RainfallStoreEntry decode(ModemResponseMessage modemResponseMessage) {
        RainfallStoreEntry entry = new RainfallStoreEntry ();
        double todayRainfall = modemResponseMessage.getData ().readUnsignedShort ()/10.0;
        double ranfall = modemResponseMessage.getData ().readUnsignedShort ()/10.0;
        double yesterdayRainfall = modemResponseMessage.getData ().readUnsignedShort ()/10.0;
        double totalRainfall = modemResponseMessage.getData ().readUnsignedShort ()/10.0;
        double hourRainfall = modemResponseMessage.getData ().readUnsignedShort ()/10.0;
        double lastHourRainfall = modemResponseMessage.getData ().readUnsignedShort ()/10.0;
        entry.setRain (todayRainfall);
        entry.setYesterdayRainfall(yesterdayRainfall);
        entry.setTotalRainfall(totalRainfall);
        entry.setHourRainfall(hourRainfall);
        entry.setLastHourRainfall(lastHourRainfall);
        return entry;
    }

    @Override
    public boolean checkError(ModemResponseMessage modemResponseMessage) {
        return modemResponseMessage.getData ().readableBytes () == 12;
    }
    @Override
    protected ModemRequestMessage getCommand(TaskEntity taskEntity) {
        return getFactory ().getRequestMessage (getConfig ());
    }

    @Override
    protected void disposable() {

    }
}
